package com.mt.order.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.order.entity.OrderDetailEntity;
import com.mt.order.mapper.OrderDetailEntityMapper;

@Component
public class OrderDetailServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(OrderDetailServiceModel.class);

    @Autowired
    private OrderDetailEntityMapper orderDetailEntityMapper;

    public ServiceResult<List<OrderDetailEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.orderDetailEntityMapper, "Property 'orderDetailEntityMapper' is required.");
        ServiceResult<List<OrderDetailEntity>> result = new ServiceResult<List<OrderDetailEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.orderDetailEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderDetailServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<OrderDetailEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.orderDetailEntityMapper, "Property 'orderDetailEntityMapper' is required.");
        ServiceResult<OrderDetailEntity> result = new ServiceResult<OrderDetailEntity>();
        try {
            result.setResult(this.orderDetailEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderDetailServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(OrderDetailEntity orderDetailEntity) {
        Assert.notNull(this.orderDetailEntityMapper, "Property 'orderDetailEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.orderDetailEntityMapper.insert(orderDetailEntity);
            if (save > 0) {
                id = orderDetailEntity.getOrderDetailId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderDetailServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(OrderDetailEntity orderDetailEntity) {
        Assert.notNull(this.orderDetailEntityMapper, "Property 'orderDetailEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.orderDetailEntityMapper.updateByPrimaryKeySelective(orderDetailEntity);
            if (save > 0) {
                id = orderDetailEntity.getOrderDetailId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderDetailServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(OrderDetailEntity orderDetailEntity) {
        Assert.notNull(this.orderDetailEntityMapper, "Property 'orderDetailEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.orderDetailEntityMapper.updateByPrimaryKeySelective(orderDetailEntity);
            if (save > 0) {
                id = orderDetailEntity.getOrderDetailId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderDetailServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer orderDetailId) {
        Assert.notNull(this.orderDetailEntityMapper, "Property 'orderDetailEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.orderDetailEntityMapper.deleteByPrimaryKey(orderDetailId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderDetailServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
